import sortedcontainers
class Solution(object):
    def goodTriplets(self, nums1, nums2):
        p1 = {}
        p2 = {}
        d = {num: index for index, num in enumerate(nums2)}
        st1 = sortedcontainers.SortedList()
        ans = 0
        for num in nums1:
            index2 = d[num]
            r = st1.bisect_right(index2)
            st1.add(index2)
            p1[num] = r
        st2 = sortedcontainers.SortedList()
        for num in nums1[::-1]:
            index2 = d[num]
            r = st2.bisect_right(-index2)
            st2.add(-index2)
            p2[num] = r
        for num in nums1:
            ans += p1[num] * p2[num]
        return ans


data = Solution()
nums1 = [2,0,1,3]
nums2 = [0,1,2,3]
print(data.goodTriplets(nums1, nums2))